#include<bits/stdc++.h>
using namespace std;
#define int long long
int read(){
    int n=0,f=1;
    char ch=getchar();
    while(!isdigit(ch)){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(isdigit(ch)){
        n=n*10+ch-'0';
        ch=getchar();
    }
    return n*f;
}
int n;
int a[200005];
void work(){
    vector<vector<int>> dp;
    //dp.clear();
    dp.resize(n+1);
    int mx=*max_element(a+1,a+n+1);
    for(int i=0;i<=n;i++)dp[i].resize(mx+1);
    for(int i=1;i<=n;i++){
        for(int j=0;j<=mx;j++)dp[i][j]=-(1ll<<60);
    }
    dp[1][0]=0;
    for(int i=2;i<=n;i++){
        for(int j=0;j<=mx;j++){
            dp[i][j]=max(dp[i][j],dp[i-1][j]+(a[i]==a[i-1]?a[i]:0ll));
            dp[i][a[i-1]]=max(dp[i][a[i-1]],dp[i-1][j]+(a[i]==j?a[i]:0ll));
            //one a[i-1].
            //other j.
        }
    }
    int res=0;
    for(int i=0;i<=mx;i++)res=max(res,dp[n][i]);
    cout<<res<<"\n";
}
int rdp[1000005];
void solve(){
    n=read();
    for(int i=1;i<=n;i++)a[i]=read();
    //if(n<=2000){work();return;}
    int mx=*max_element(a+1,a+n+1);
    //if(mx<=10){work();return;}
    //if(n>2000&&mx>10)return;
    rdp[0]=0;
    for(int i=1;i<=mx;i++)rdp[i]=-(1ll<<60);
    int delta=0;
    int cmx=0;
    for(int i=2;i<=n;i++){
        int p=-(1ll<<60);
        p=max(p,cmx+delta);/*
        for(int j=0;j<=mx;j++){
            p=max(p,rdp[j]+delta);
        }*/
        p=max(rdp[a[i]]+delta+a[i],p);
        if(a[i]==a[i-1]){delta+=a[i];}
        cmx=max(cmx,p-delta);
        rdp[a[i-1]]=max(rdp[a[i-1]],p-delta);
    }
    cout<<*max_element(rdp,rdp+mx+1)+delta<<"\n";
}
signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int t=read();
    while(t--)solve();
    return 0;
}
//99 bugs in the code
//hunt one down
//toss it around
//101 bugs in the code
